package com.zhijiang.hospital.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhijiang.hospital.entity.MedicalTechnollogy;
import com.zhijiang.hospital.mapper.OutpatientExaminationsMapper;
import com.zhijiang.hospital.service.MedicalTechnologyService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.NumberUtils;
import com.zhijiang.util.PageList;
import com.zhijiang.util.TimeUtil;
import com.zhijiang.util.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MedicalTechnologyServiceImpl implements MedicalTechnologyService {
    @Autowired
    RedisService redisService;
    @Resource
    OutpatientExaminationsMapper outpatientExaminationsMapper;

    @Override
    public Map<String, Object> getOutpatientExaminations(BaseQuery baseQuery) {
        String endTime = DateUtil.formatDate(new Date());
        Boolean aBoolean = redisService.hasKey(baseQuery.getStartTime());
        if (aBoolean && baseQuery.getEndTime().equals(endTime)) {
            List<MedicalTechnollogy> keyObjectValue = redisService.getKeyObjectValue(baseQuery.getStartTime(), List.class);
            List<String> collect = keyObjectValue.stream().map(MedicalTechnollogy::getOutpatientCount).collect(Collectors.toList());
            int sum = collect.stream().mapToInt(Integer::parseInt).sum();
            MedicalTechnollogy medicalTechnollogy = new MedicalTechnollogy();
            medicalTechnollogy.setDeptName("合计");
            medicalTechnollogy.setOutpatientCount(String.valueOf(sum));
            ArrayList<Object> list = new ArrayList<>();
            list.add(medicalTechnollogy);
            HashMap<String, Object> map = new HashMap<>();
            map.put("heji", list);
            PageInfo<MedicalTechnollogy> medicalTechnollogyPageInfo = new PageInfo<>(keyObjectValue);
            Integer pages = NumberUtils.totalPages(medicalTechnollogyPageInfo.getTotal(), baseQuery.getPageSize());
            PageList<MedicalTechnollogy> medicalTechnollogyPageList = new PageList<>(medicalTechnollogyPageInfo.getTotal(), medicalTechnollogyPageInfo.getList(), baseQuery.getPage(), baseQuery.getPageSize(), pages);
            map.put("jihe", medicalTechnollogyPageList);
            return map;
        }
        PageHelper.startPage(baseQuery.getPage(), baseQuery.getPageSize());
        List<MedicalTechnollogy> outpatientExaminations = outpatientExaminationsMapper.getList(baseQuery);
        List<String> collect = outpatientExaminations.stream().map(MedicalTechnollogy::getOutpatientCount).collect(Collectors.toList());
        int sum = collect.stream().mapToInt(Integer::parseInt).sum();
        MedicalTechnollogy medicalTechnollogy = new MedicalTechnollogy();
        medicalTechnollogy.setDeptName("合计");
        medicalTechnollogy.setOutpatientCount(String.valueOf(sum));
        ArrayList<Object> list = new ArrayList<>();
        list.add(medicalTechnollogy);
        HashMap<String, Object> map = new HashMap<>();
        map.put("heji", list);
        PageInfo<MedicalTechnollogy> medicalTechnollogyPageInfo = new PageInfo<>(outpatientExaminations);
        Integer pages = NumberUtils.totalPages(medicalTechnollogyPageInfo.getTotal(), baseQuery.getPageSize());
        PageList<MedicalTechnollogy> medicalTechnollogyPageList = new PageList<>(medicalTechnollogyPageInfo.getTotal(), medicalTechnollogyPageInfo.getList(), baseQuery.getPage(), baseQuery.getPageSize(), pages);
        map.put("jihe", medicalTechnollogyPageList);
        return map;
    }

    @Override
    public List<MedicalTechnollogy> getYearData() {
        DateTime dateTime = DateUtil.beginOfYear(new Date());
        DateTime dateTime1 = DateUtil.endOfYear(new Date());
        String startTime = Convert.toStr(dateTime);
        String endTime = Convert.toStr(dateTime1);
        List<MedicalTechnollogy> outpatientExaminations = outpatientExaminationsMapper.getYearData(startTime, endTime);
        return outpatientExaminations;
    }

    @Override
    public List<MedicalTechnollogy> getLastYearData() {
        Date nextYearPreDay = TimeUtil.getNextYearPreDay();
        DateTime dateTime3 = DateUtil.beginOfYear(nextYearPreDay);
        DateTime dateTime4 = DateUtil.endOfYear(nextYearPreDay);
        String startTime = TimeUtil.format(dateTime3);
        String endTime = TimeUtil.format(dateTime4);
        List<MedicalTechnollogy> outpatientExaminations = outpatientExaminationsMapper.getYearData(startTime, endTime);
        return outpatientExaminations;
    }
}
